#include <stdio.h>
#include "ifi_picdefs.h"
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "UserApi.h"

/*******************************************************************************
* FUNCTION NAME: GetDigitalInput
* PURPOSE:       Get the digital value on the port specified
* CALLED FROM:   Anywhere
* ARGUMENTS:     ucNum - port to read
* RETURNS:       unsigned char
*******************************************************************************/
unsigned char GetDigitalInput(unsigned char ucNum)
{
   switch(ucNum)
   {
   case 1:
      return rc_dig_in01;
   case 2:
      return rc_dig_in02;
   case 3:
      return rc_dig_in03;
   case 4:
      return rc_dig_in04;
   case 5:
      return rc_dig_in05;
   case 6:
      return rc_dig_in06;
   case 7:
      return rc_dig_in07;
   case 8:
      return rc_dig_in08;
   case 9:
      return rc_dig_in09;
   case 10:
      return rc_dig_in10;
   case 11:
      return rc_dig_in11;
   case 12:
      return rc_dig_in12;
   case 13:
      return rc_dig_in13;
   case 14:
      return rc_dig_in14;
   case 15:
      return rc_dig_in15;
   case 16:
      return rc_dig_in16;
   }
   return 0;
}

/*******************************************************************************
* FUNCTION NAME: SetDigitalOutput
* PURPOSE:       Set the digital value on the port specified
* CALLED FROM:   Anywhere
* ARGUMENTS:     ucNum - port to set
*                ucValue - value to set
* RETURNS:       void
*******************************************************************************/
void SetDigitalOutput(unsigned char ucNum, unsigned char ucValue)
{
   switch(ucNum)
   {
   case 1:
      rc_dig_out01 = ucValue;
      return;
   case 2:
      rc_dig_out02 = ucValue;
      return;
   case 3:
      rc_dig_out03 = ucValue;
      return;
   case 4:
      rc_dig_out04 = ucValue;
      return;
   case 5:
      rc_dig_out05 = ucValue;
      return;
   case 6:
      rc_dig_out06 = ucValue;
      return;
   case 7:
      rc_dig_out07 = ucValue;
      return;
   case 8:
      rc_dig_out08 = ucValue;
      return;
   case 9:
      rc_dig_out09 = ucValue;
      return;
   case 10:
      rc_dig_out10 = ucValue;
      return;
   case 11:
      rc_dig_out11 = ucValue;
      return;
   case 12:
      rc_dig_out12 = ucValue;
      return;
   case 13:
      rc_dig_out13 = ucValue;
      return;
   case 14:
      rc_dig_out14 = ucValue;
      return;
   case 15:
      rc_dig_out15 = ucValue;
      return;
   case 16:
      rc_dig_out16 = ucValue;
      return;
   }
}

/*******************************************************************************
* FUNCTION NAME: SetupDigitalInput
* PURPOSE:       Setup the port as input
* CALLED FROM:   Anywhere
* ARGUMENTS:     ucNum - port to read
* RETURNS:       void
*******************************************************************************/
void SetupDigitalInput(unsigned char ucNum)
{
   switch(ucNum)
   {
   case 1:
      IO1 = INPUT;
      return;
   case 2:
      IO2 = INPUT;
      return;
   case 3:
      IO3 = INPUT;
      return;
   case 4:
      IO4 = INPUT;
      return;
   case 5:
      IO5 = INPUT;
      return;
   case 6:
      IO6 = INPUT;
      return;
   case 7:
      IO7 = INPUT;
      return;
   case 8:
      IO8 = INPUT;
      return;
   case 9:
      IO9 = INPUT;
      return;
   case 10:
      IO10 = INPUT;
      return;
   case 11:
      IO11 = INPUT;
      return;
   case 12:
      IO12 = INPUT;
      return;
   case 13:
      IO13 = INPUT;
      return;
   case 14:
      IO14 = INPUT;
      return;
   case 15:
      IO15 = INPUT;
      return;
   case 16:
      IO16 = INPUT;
      return;
   }
}

/*******************************************************************************
* FUNCTION NAME: SetupDigitalOutput
* PURPOSE:       Setup the port as output
* CALLED FROM:   Anywhere
* ARGUMENTS:     ucNum - port to read
* RETURNS:       void
*******************************************************************************/
void SetupDigitalOutput(unsigned char ucNum)
{
   switch(ucNum)
   {
   case 1:
      IO1 = OUTPUT;
      return;
   case 2:
      IO2 = OUTPUT;
      return;
   case 3:
      IO3 = OUTPUT;
      return;
   case 4:
      IO4 = OUTPUT;
      return;
   case 5:
      IO5 = OUTPUT;
      return;
   case 6:
      IO6 = OUTPUT;
      return;
   case 7:
      IO7 = OUTPUT;
      return;
   case 8:
      IO8 = OUTPUT;
      return;
   case 9:
      IO9 = OUTPUT;
      return;
   case 10:
      IO10 = OUTPUT;
      return;
   case 11:
      IO11 = OUTPUT;
      return;
   case 12:
      IO12 = OUTPUT;
      return;
   case 13:
      IO13 = OUTPUT;
      return;
   case 14:
      IO14 = OUTPUT;
      return;
   case 15:
      IO15 = OUTPUT;
      return;
   case 16:
      IO16 = OUTPUT;
      return;
   }
}

/*******************************************************************************
* FUNCTION NAME: SetMotor
* PURPOSE:       Sets the motor to the specified pulse width
* CALLED FROM:   Anywhere
* ARGUMENTS:     motor - 1 based motor number
*                value - 0-255 -value of the pulse width
* RETURNS:       void
*******************************************************************************/
void SetMotor(unsigned char motor, unsigned char value)
{
   SetPWM(motor, value);
}

/*******************************************************************************
* FUNCTION NAME: SetServo
* PURPOSE:       Sets the servo to the specified pulse width
* CALLED FROM:   Anywhere
* ARGUMENTS:     motor - 1 based servo number
*                value - 0-255 -value of the pulse width
* RETURNS:       void
*******************************************************************************/
void SetServo(unsigned char servo, unsigned char value)
{
   SetPWM(servo, value);
}

/*******************************************************************************
* FUNCTION NAME: SetPWM
* PURPOSE:       Sets the PWM to the specified pulse width
* CALLED FROM:   Anywhere
* ARGUMENTS:     motor - 1 based servo number
*                value - 0-255 -value of the pulse width
* RETURNS:       void
*******************************************************************************/
void SetPWM(unsigned char ucNum, unsigned char ucValue)
{
   switch(ucNum)
   {
   case 1:
      pwm01 = ucValue;
      return;
   case 2:
      pwm02 = ucValue;
      return;
   case 3:
      pwm03 = ucValue;
      return;
   case 4:
      pwm04 = ucValue;
      return;
   case 5:
      pwm05 = ucValue;
      return;
   case 6:
      pwm06 = ucValue;
      return;
   case 7:
      pwm07 = ucValue;
      return;
   case 8:
      pwm08 = ucValue;
      return;
   }
}

/*******************************************************************************
* FUNCTION NAME: Setup_Who_Controls_Pwms
* PURPOSE:       Each parameter specifies what processor will control the pwm.  
*                 
* CALLED FROM:   User_Initialization
*     Argument             Type    IO   Description
*     --------             ----    --   -----------
*     pwmSpec1              int     I   USER/MASTER (defined in ifi_aliases.h)
*     pwmSpec2              int     I   USER/MASTER
*     pwmSpec3              int     I   USER/MASTER
*     pwmSpec4              int     I   USER/MASTER
*     pwmSpec5              int     I   USER/MASTER
*     pwmSpec6              int     I   USER/MASTER
*     pwmSpec7              int     I   USER/MASTER
*     pwmSpec8              int     I   USER/MASTER
* RETURNS:       void
*******************************************************************************/
void Setup_Who_Controls_Pwms(int pwmSpec1,int pwmSpec2,int pwmSpec3,int pwmSpec4,
                             int pwmSpec5,int pwmSpec6,int pwmSpec7,int pwmSpec8)
{
   txdata.pwm_mask = 0xFF;         /* Default to master controlling all PWMs. */
   if (pwmSpec1 == USER)           /* If User controls PWM1 then clear bit0. */
      txdata.pwm_mask &= 0xFE;      /* same as txdata.pwm_mask = txdata.pwm_mask & 0xFE; */
   if (pwmSpec2 == USER)           /* If User controls PWM2 then clear bit1. */
      txdata.pwm_mask &= 0xFD;
   if (pwmSpec3 == USER)           /* If User controls PWM3 then clear bit2. */
      txdata.pwm_mask &= 0xFB;
   if (pwmSpec4 == USER)           /* If User controls PWM4 then clear bit3. */
      txdata.pwm_mask &= 0xF7;
   if (pwmSpec5 == USER)           /* If User controls PWM5 then clear bit4. */
      txdata.pwm_mask &= 0xEF;
   if (pwmSpec6 == USER)           /* If User controls PWM6 then clear bit5. */
      txdata.pwm_mask &= 0xDF;
   if (pwmSpec7 == USER)           /* If User controls PWM7 then clear bit6. */
      txdata.pwm_mask &= 0xBF;
   if (pwmSpec8 == USER)           /* If User controls PWM8 then clear bit7. */
      txdata.pwm_mask &= 0x7F;
}
